﻿using Sasa.QualityTools.CodeAnalysis.Common;
using Sasa.QualityTools.CodeAnalysis.Core.Properties;
using Sasa.QualityTools.CodeAnalysis.Core.Utilities;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;

namespace Sasa.QualityTools.CodeAnalysis.Core.Models
{
    [SuppressMessage("Microsoft.Naming", "CA1724")]
    public class CodeInspector : CodeAnalyzerBase<FxCopReport, FxCopErrorCodes>
    {
        protected override void ResolveMissingAssemblies()
        {
            var resolver = new MissingAssemblyResolver(MissingAssembliesCachePath, ReferenceAssemblyPaths);
            Report.Load(GetReportPath());
            resolver.Resolve(Report.Errors);
        }

        protected override FxCopErrorCodes Convert(int commandExitCode)
        {
            return (FxCopErrorCodes)commandExitCode;
        }

        protected override bool HasAssemblyReferencesError(FxCopErrorCodes result)
        {
            return result.HasFlag(FxCopErrorCodes.AssemblyReferencesError);
        }

        protected override bool HasAnalysisError(FxCopErrorCodes result)
        {
            return result.HasFlag(FxCopErrorCodes.AnalysisError);
        }

        protected override string GetReportPath()
        {
            return Regex.Match(CommandOption, @"/o:""(.+?)""").Groups[1].Value;
        }
    }
}
